package com.sanrenzu.everybodytv.http;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sanrenzu.everybodytv.bean.sms.IntentBean;
import com.sanrenzu.everybodytv.bean.sms.live.ZhiBoBean;
import com.sanrenzu.everybodytv.config.UrlConfig;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by My on 2016/8/30.
 */
public class HttpUtilsCommend {

    //保证http访问工具仅有一个
    private   static  volatile HttpUtilsCommend sinleton;
    private Retrofit retrofit;
    private IDataBeanCommend iDataBeanCommend;

    private HttpUtilsCommend() {
        retrofit = createRetrofit();
        iDataBeanCommend = createIDataBean();
    }
    private IDataBeanCommend creatChunbo() {
        IDataBeanCommend iDataBeanCommend =   retrofit.create(IDataBeanCommend.class);
        return iDataBeanCommend;
    }

    //同步锁定http访问类，保证每次仅一个，锁定后通过类的实例继续调用
    public static HttpUtilsCommend getInstance(){
        if(sinleton==null){
            synchronized (HttpUtilsCommend.class){
                if (sinleton==null){
                    sinleton = new HttpUtilsCommend();
                }
            }
        }
        return sinleton;
    }

    /**
     * Retrofit 实例化
     * @return
     */
    private Retrofit createRetrofit(){
        return new Retrofit.Builder()
                .baseUrl(UrlConfig.Path.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(createGson()))
                .build();
    }
    /**
     * Gson实例化
     * @return
     */
    private Gson createGson(){
        Gson gson = new GsonBuilder()
                //序列化不为空
                .serializeNulls()
                .create();
        return gson;
    }

    /**
     * 接口的实例化
     * @return
     */
    private IDataBeanCommend createIDataBean(){
        IDataBeanCommend iDataBeanCommend =   retrofit.create(IDataBeanCommend.class);
        return iDataBeanCommend;
    }

    /**
     * 创造首页的数据
     * @param callback
     */
    public void getRecommendBean(Callback<ResponseBody> callback){
        Call<ResponseBody> call = iDataBeanCommend.getCommendBean();
        call.enqueue(callback);
    }

    //广告栏的数据
    public void getRecommendViewPager(Callback<ResponseBody> callback){
        Call<ResponseBody> call =iDataBeanCommend.getCommendViewPager();
        call.enqueue(callback);
    }

    //点击传值的数据
    public void getRecommendIntentGirdView(String name,Callback<IntentBean> callback){
        Call<IntentBean> call = iDataBeanCommend.getCommendIntentGirdView(name);
        call.enqueue(callback);
    }

    //直播的数据
    public void getRecommendIntentZhiBo(String id, Callback<ZhiBoBean> callback){
        Call<ZhiBoBean> call = iDataBeanCommend.getCommendZhiBo(id);
        call.enqueue(callback);
    }
}
